package org.stvd.controller.workflow;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.dto.SubmitResult;
import org.stvd.core.util.MathUtil;
import org.stvd.entities.workflow.WfReflect;
import org.stvd.service.workflow.WfReflectService;

@Controller
@RequestMapping("/workflow/wfreflect")
public class WfReflectController extends BaseController{

    @Autowired
    WfReflectService wfReflectService;
    
    @RequestMapping(value="management")
    public String onPageLoad(@ModelAttribute("wfReflect") WfReflect wfReflect){
        map.put("queryResult", onQuery(pageNo,pageSize));
        return "/workflow/wfreflect/WfReflectManagement.html";
    }
    
     public QueryResult<WfReflect> onQuery(Integer page,Integer rows){
         return wfReflectService.getQueryResult((page-1)*rows, rows);
     }
    
    @RequestMapping(value="add",method=RequestMethod.GET)
    public String onAddLoad(ModelMap map,
            @ModelAttribute("wfReflect") WfReflect wfReflect,String enterFlag){
        
        if(enterFlag != null){
            map.put("saveMsg", "");
        }
        
        return "/workflow/wfreflect/WfReflectAdd.html";
    }
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    public String onAddAction(
            HttpServletRequest request,ModelMap map,
            @Valid @ModelAttribute("wfReflect") WfReflect wfReflect,
            BindingResult bindingResult) throws Exception{
        
        if(bindingResult.hasErrors() ){
            map.put("errorList", bindingResult.getAllErrors());
            return "/workflow/wfreflect/WfReflectAdd.html";
        }
        
        wfReflectService.insert(wfReflect);
        
        map.clear();
        return "redirect:management";
//        return "redirect:add?enterFlag";
    }
    
    @RequestMapping(value="edit-{ids}",method=RequestMethod.GET)
    public String onEditLoad(
            HttpServletRequest request,ModelMap map,
            @PathVariable String ids){
        Integer id = MathUtil.parseInt(ids);
        WfReflect wfReflect = wfReflectService.findByPk(WfReflect.class, id);
        if(wfReflect == null){
            wfReflect = new WfReflect();
        }
        map.put("wfReflect", wfReflect);
        return "/workflow/wfreflect/WfReflectEdit.html";
    }
    
    @RequestMapping(value="edit-{ids}",method=RequestMethod.POST)
    public String onEditActive(
            HttpServletRequest request,ModelMap map,
            @PathVariable String ids,
            @Valid @ModelAttribute("wfReflect") WfReflect wfReflect,
            BindingResult bindingResult) throws Exception{
                
        if(bindingResult.hasErrors() ){
            map.put("errorList", bindingResult.getAllErrors());
            return "/workflow/wfreflect/WfReflectEdit.html";
        }
        wfReflectService.update(wfReflect);
        
        map.clear();
        return "redirect:management";
    }
        
    @RequestMapping(value="del",method=RequestMethod.POST)
     @ResponseBody
    public SubmitResult onProDelete(
            Integer id,String title) {
         SubmitResult submitResult = new SubmitResult() ;
         try{
             wfReflectService.delete(WfReflect.class,id);
             submitResult.setMessage("删除‘"+title+"’成功！");
            submitResult.setSuccess(true);
         }catch (Exception e) {
             e.printStackTrace();
             submitResult.setMessage("删除"+title+"失败！<br/>失败原因："+e.getMessage());
         }
         return submitResult ;
    }
}
